using System;
using System.Windows.Forms;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.Faces.Base;
using Habanero.Faces.Win;

namespace FireStarterModeller.UI
{
    public class DMClassTreeViewSelectorControl : UserControlWin, IFormControl, IBOParentChildrenSelector
    {
        private readonly ITreeView _treeView;
        private readonly TreeViewController _treeViewController;
        private DMSolution _dmSolution;
        private ImageList _imageList;

        public DMClassTreeViewSelectorControl(IControlFactory controlFactory)
        {

            ControlFactory = controlFactory;
            _treeView = controlFactory.CreateTreeView();

            _imageList = new ImageList();
            _imageList.Images.Add("None", FMResources.Folder);
            _imageList.Images.Add("Module", FMResources.Module);
            _imageList.Images.Add("Entity", FMResources.entityimage2);
            ((TreeViewWin)_treeView).ImageList = _imageList;
            _treeViewController = new TreeViewController(_treeView);
            BorderLayoutManager layoutManager = controlFactory.CreateBorderLayoutManager(this);
            layoutManager.AddControl(_treeView, BorderLayoutManager.Position.Centre);
//            this.Controls.Add(_treeView);
//            _treeView.Dock = DockStyle.Fill;
            _treeViewController.BusinessObjectSelected += treeViewController_BusinessObjectSelected;
            AutoSelectFirstItem = true;

          
            _treeViewController.SetupNodeWithBusinessObject = ((node, businessObject) => SetNodeImage(businessObject, node));

            _treeViewController.SetupNodeWithRelationship = SetupNodeWithRelationship;

        }

        private void SetupNodeWithRelationship(ITreeNode node, IRelationship relationship)
        {
            string imageName = "None";
            node.SelectedImageIndex = GetImageIndex(imageName);
            node.ImageIndex = GetImageIndex(imageName);
        }

        private void SetNodeImage(IBusinessObject businessObject, ITreeNode node)
        {
            string imageName = null;
            Type type = businessObject.GetType();
            if (type == typeof(DMAssembly))
            {
                imageName = "Module";
            }
            else if (type == typeof(DMClass) )
            {
                imageName = "Entity";
            }
            else
            {
                imageName = "None";
            }           
            node.SelectedImageIndex = GetImageIndex(imageName);
            node.ImageIndex = GetImageIndex(imageName);
            //TODO Mark 20 Mar 2009: Surely the logic from GetImageIndex below can be used to create implementations for the missing VWG methods commented out below.
            //node.SelectedImageKey = imageName;
            //node.ImageKey = imageName;
        }


        private int GetImageIndex(string imageName)
        {
            return _imageList.Images.IndexOfKey(imageName);
        }

        ~DMClassTreeViewSelectorControl()
        {
            TreeViewController.BusinessObjectSelected -= treeViewController_BusinessObjectSelected;
        }
        public IControlFactory ControlFactory { get; private set; }

        public DMSolution DMSolution
        {
            get { return _dmSolution; }
            set
            {
                try
                {
                    _dmSolution = value;
                    TreeViewController.LoadTreeView(_dmSolution.Relationships["Assemblies"], 3, 3);
                }
                catch (Exception ex)
                {
                    GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
                }
                //MessageBox.Show(_dmSolution.Assemblies[0].Classes[0].Properties[0].PropRules.Count.ToString());
                //_treeViewController.LoadTreeView(_dmSolution, 4);
            }
        }

        #region IBOParentChildrenSelector Members

        public IBusinessObject BusinessObject
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        #endregion

        #region IBOColSelector

        public event EventHandler<BOEventArgs> BusinessObjectSelected;

        public void Clear()
        {
        }

        public IBusinessObject GetBusinessObjectAtRow(int row)
        {
            return null;
        }

        public IBusinessObjectCollection BusinessObjectCollection
        {
            get { return DMSolution.Assemblies; }
            set { }
        }

        public IBusinessObject SelectedBusinessObject
        {
            //get { return this._treeViewController.SelectedBusinessObject; }
            //set { this.GridControl.SelectedBusinessObject = value; }
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        public int NoOfItems
        {
            //get { return this.GridControl.NoOfItems; }
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        public ITreeView TreeView
        {
            get { return _treeView; }
        }

        public TreeViewController TreeViewController
        {
            get { return _treeViewController; }
        }

        /// <summary>
        ///             Gets and sets whether this selector autoselects the first item or not when a new collection is set.
        /// </summary>
        public bool AutoSelectFirstItem { get; set; }

        public DMClass DMClass
        {
            get { throw new NotImplementedException(); }
            set
            {
                DMAssembly assembly = value.Assembly;
                ITreeNode treeNode = _treeViewController.GetBusinessObjectTreeNode(assembly);
                if (treeNode != null)
                {
                    treeNode.ExpandAll();
                    _treeViewController.SelectObject(value);

                }
            }
        }

        #endregion

        #region IFormControl Members

        public void SetForm(IFormHabanero form)
        {

        }

        #endregion

//        public event EventHandler<BOEventArgs<DMClass>> ClassSelected;

        private void treeViewController_BusinessObjectSelected(object sender, BOEventArgs e)
        {
            try
            {
                IBusinessObject businessObject = e.BusinessObject;
                if (businessObject is DMClass)
                {
                    FireEventClassSelected((DMClass) businessObject);
                }
            }
            catch (Exception ex)
            {
                GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
            }
        }

        private void FireEventClassSelected(DMClass dmClass)
        {
            try
            {
                if (BusinessObjectSelected != null)
                {
                    BusinessObjectSelected(this, new BOEventArgs(dmClass));
                }
            }
            catch (Exception ex)
            {
                GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
            }
        }

        public void CleanUp()
        {
            _treeViewController.CleanUp();
        }
    }
}
